<template>
  <div class="main">
    <!-- 条件查询 -->
    <el-card class="card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="condtion">条件查询</span>
      </div>
      <!-- 条件查询内容 -->
      <div>
        <el-form
          size="mini"
          :inline="true"
          :model="searchFrom"
          class="demo-form-inline"
        >
          <el-form-item label="作者" class="users">
            <el-input v-model="searchFrom.author" placeholder="作者"></el-input>
          </el-form-item>
          <el-form-item label="标题" class="users">
            <el-input v-model="searchFrom.title" placeholder="标题"></el-input>
          </el-form-item>

          <el-form-item label="编辑标题" class="users">
            <el-select
              v-model="searchFrom.editorType"
              placeholder="请选择"
              size="mini"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="编辑类型" class="users">
            <el-input placeholder="编辑类型"></el-input>
          </el-form-item> -->
          <el-form-item class="usered">
            <el-button @click="reset">重置</el-button>
            <el-button type="primary" @click="doSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <!-- 文章列表table表单 -->
    <el-card shadow="never" class="card">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="default" @click="add"
          >新增文章</el-button
        >
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="1000"
        v-loading="loading"
        border
      >
        <el-table-column fixed type="index" width="50"> </el-table-column>
        <el-table-column
          prop="title"
          label="文章标题"
          width="300"
          heigth="133"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="province"
          label="展示图片"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <img
              style="width: 100px; height: 100px"
              :src="scope.row.coverImg"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          width="300"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="author"
          label="文章作者"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column label="是否展示" width="150">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isShow"
              active-color="#409EFF"
              inactive-color="#DCEFE6"
              :active-value="1"
              :inactive-value="0"
              @change="changeStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="内容" width="200" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.editorType == 0" class="frame">富文本框</div>
            <div v-else class="frame">Markdown</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click="edit(scope.row)"
              type="text"
              size="small"
              style="color: aqua !important"
            >
              编辑
            </el-button>
            <el-button
              @click="deleteRow(scope.row)"
              type="text"
              size="small"
              style="color: red !important"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import articleApi from '@/api/article.js'
import mixin from '@/mixins'

export default {
  mixins: [mixin],
  data() {
    return {
      searchFrom: {}, //输入框里的对象
      form: {}, //实际搜索要用的表单
      tableData: [], //文章列表数据
      loading: false,
      options: [
        {
          value: '0',
          label: '富文本',
        },
        {
          value: '1',
          label: 'MarkDown',
        },
      ],
      value: '',
    }
  },
  created() {
    this.getArticleList()
  },
  mounted() {},
  methods: {
    //编辑文章
    edit(row) {
      console.log(row, '编辑')
      this.$router.push({ name: 'EditArticle', params: { id: row.id } })
    },
    //新增文章add
    add() {
      //跳转到新增文章页面
      this.$router.push({ path: '/content/addArticle' })
    },
    //查询
    doSearch() {
      this.page = {
        currentPage: 1, //当前页
        pageSizes: [10, 20, 30, 40],
        pageSize: 10, //每页多少条数据
        total: 0, //总数据条数
      }
      this.from = Object.assign({}, this.searchFrom)

      this.getArticleList()
      console.log(this.searchFrom, 'this.searchFrom')
      console.log(this.from, 'this.from')
    },
    //重置
    reset() {
      this.searchFrom = {}
      this.page = {
        currentPage: 1, //当前页
        pageSizes: [10, 20, 30, 40],
        pageSize: 10, //每页多少条数据
        total: 0, //总数据条数
      }
      this.getArticleList()
    },
    //删除文章
    deleteRow(row) {
      console.log(row, '删除')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        articleApi.delArticleApi(row.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.getArticleList()
          } else {
            this.$message.error(res.message)
          }
        })
      })
    },

    //获取文章列表
    getArticleList() {
      this.loading = true

      articleApi
        .getArticleListApi(this.page.currentPage, this.page.pageSize, this.from)
        .then(res => {
          console.log(res, 'res')
          if (res.success) {
            this.tableData = res.data.rows //获取到线上文字地址
            this.page.total = res.data.total //获取文章总条数
          } else {
            this.$message.error('请求失败')
          }
          console.log(this.tableData, 'this.tableData')
          this.loading = false
        })
    },
    //修改文章显示状态
    changeStatus(row) {
      //暂存一个改变之前的状态
      console.log(row, 'row')
      var ermpStatus = row.isShow === 1 ? 0 : 1 //如果失败了  0就是1  1就是0
      articleApi
        .changeShowApi({
          isShow: row.isShow,
          id: row.id,
        })
        .then(res => {
          console.log(res, 'changeShowApi.res')
          if (res.success) {
            this.$message.success('修改状态成功')
          } else {
            this.$message.error('修改状态失败，请联系管理员')
            row.isShow = ermpStatus
          }
        })
    },
    //每页多少条 改变时触发事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.pageSize = val
      this.getArticleList()
    },
    //修改页触发的事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.currentPage = val
      this.getArticleList()
    },
  },
}
</script>

<style lang="scss" scoped>
.main {
  background: #f5f5f5;
  .card {
    width: 96.5%;
    margin: 30px;
  }
}
.condtion {
  background-color: #fff;
  color: #303133;
  font-size: 10px;
}
.users {
  margin-right: 200px;
}
.usered {
  position: relative;
  top: 30px;
}
.el-card__body {
  height: 1200px;
}
.frame {
  width: 80px;
  margin: auto;
  color: #409eff;
  border-radius: 4px;
  background-color: #ecf5ff;
  border-color: #d9ecff;
}
</style>